package com.cxh.rmsq.ui.activity.self

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.ViewModelProviders
import com.cxh.rmsq.R
import com.cxh.rmsq.ui.activity.AccountSettingActivity
import com.cxh.rmsq.ui.activity.LoginActivity
import com.cxh.rmsq.ui.dialog.ClearCacheDialog
import com.cxh.rmsq.viewmodel.UserInfoViewModel
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.cxh.rmsq.ui.BaseActivity
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupAnimation
import io.rong.callkit.util.SPUtils
import io.rong.imkit.feature.resend.ResendManager

class SettingActivity : BaseActivity(), View.OnClickListener {
    private var userInfoViewModel: UserInfoViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)
        findViewById<LinearLayout>(R.id.cl_toolbar).setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)

        userInfoViewModel = ViewModelProviders.of(this).get(UserInfoViewModel::class.java)
        findViewById<LinearLayout>(R.id.iv_back).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_zhaq).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_zhgl).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_privacy).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_commonly_used).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_about_us).setOnClickListener(this)
        findViewById<TextView>(R.id.siv_clear_cache).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_login_out).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_help).setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> {
                finish()
            }

            R.id.tv_zhaq -> {
                startActivity(Intent(this@SettingActivity, SecuritySettingsActivity::class.java))
            }

            R.id.tv_zhgl -> {
                startActivity(Intent(this@SettingActivity, AccountSettingActivity::class.java))
            }

            R.id.tv_privacy -> {
//                startActivity(Intent(this@SettingActivity, PrivacyActivity::class.java))
                startActivity(Intent(this@SettingActivity, PrivacySelfActivity::class.java))
            }

            R.id.tv_commonly_used -> {
//                startActivity(Intent(this@SettingActivity, CommonlyUsedActivity::class.java))
            }

            R.id.tv_about_us -> {
                startActivity(Intent(this@SettingActivity, AboutUsActivity::class.java))
            }

            R.id.tv_login_out -> {
                // 退出
                backTips()
            }

            R.id.siv_clear_cache -> {
                showClearDialog()
            }

            R.id.tv_help -> {
                startActivity(Intent(this@SettingActivity, HelpComplaintActivity::class.java))
            }

        }
    }

    private fun showClearDialog() {
        val builder = ClearCacheDialog.Builder()
        builder.setContentMessage(getString(R.string.seal_set_account_dialog_clear_cache_message))
        val dialog = builder.build()
        dialog.show(supportFragmentManager, "clear_cache")
    }

    var popupView: BasePopupView? = null
    private fun backTips() {
        popupView = XPopup.Builder(this)
            .hasNavigationBar(false)
            .isViewMode(true)
            .isDestroyOnDismiss(true)
            .hasBlurBg(true) //是否设置背景为高斯模糊背景。默认为false
            .dismissOnTouchOutside(false) //设置点击弹窗外面是否关闭弹窗，默认为true
            .autoDismiss(false) //设置当操作完毕后是否自动关闭弹窗，默认为true。比如：点击Confirm弹窗的确认按钮默认是关闭弹窗，如果为false，则不关闭
            .popupAnimation(PopupAnimation.NoAnimation) // 为弹窗设置内置的动画器，默认情况下，已经为每种弹窗设置了效果最佳的动画器；如果你不喜欢，仍然可以修改。
            .asConfirm("", "确定要退出当前账号吗？",
                "确定", "取消",
                {
                    popupView?.dismiss()
                }, {
                    finishLogout(false)
                    popupView?.dismiss()
                }, false/*, R.layout.custom_confirm_pop*/
            ).show()
    }

    private fun finishLogout(isAccountDeleted: Boolean) {
        ResendManager.getInstance().removeAllResendMessage()
        logout(isAccountDeleted)
        // 通知退出
        sendLogoutNotify()
        SPUtils.clear(applicationContext)
        com.blankj.utilcode.util.SPUtils.getInstance().clear()
        startActivity(Intent(this, LoginActivity::class.java))
        finish()
    }

    private fun logout(isAccountDeleted: Boolean) {
        if (userInfoViewModel != null) {
            if (isAccountDeleted) {
                userInfoViewModel!!.accountDelete()
            } else {
                userInfoViewModel!!.logout()
            }
        }
    }

}